﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;

namespace DAL
{
    //valerá ter esta classe visto que a implementação é sempre é LINQ
    public class DataAccessLayerFactory
    {
        static readonly Dictionary<String, Type> _DalImplementationsTypes = new Dictionary<string, Type>();

        static DataAccessLayerFactory()
        {
            NameValueCollection appSettings = null; //= ConfigurationManager.AppSettings;
            foreach (String key in appSettings.AllKeys)
            {
                if (!key.StartsWith("DAL_")) continue;
                Type t = Type.GetType(appSettings[key]);
                _DalImplementationsTypes.Add(key.Substring(4), t);
            }
        }

        public static IDataAccessLayer GetDataAccessLayer(String dalImplName)
        {
            if (_DalImplementationsTypes.ContainsKey(dalImplName))
            {
                Type t = _DalImplementationsTypes[dalImplName];
                return (IDataAccessLayer)Activator.CreateInstance(t);
            }
            return null;
        }
    }
}
